package com.jiandian.zh.api.common.controller;

import com.github.pagehelper.PageInfo;
import com.jiandian.zh.api.common.domain.BaseDomain;
import com.jiandian.zh.api.common.dto.BaseResult;
import com.jiandian.zh.api.common.service.BaseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * 通用 Controller
 */

public abstract class BaseController<T extends BaseDomain, S extends BaseService> {

    /**
     * 注入业务逻辑层
     */
    @Autowired(required = false)
    protected S service;

    @ResponseBody
    @GetMapping("page/{pageNum}/{pageSize}")
    public BaseResult page(@PathVariable(required = true) int pageNum,
                           @PathVariable(required = true) int pageSize,
                            T whereObject) {
        System.out.println(123);
        PageInfo pageInfo = service.selectPage(pageNum, pageSize, whereObject);
        return BaseResult.success(pageInfo);
    }

    @ResponseBody
    @GetMapping(value = "{id}")
    public BaseResult get(@PathVariable("id") String id) {
        BaseDomain baseDomain = service.selectByPrimaryKey(id);
        return BaseResult.success(baseDomain);
    }

    @ResponseBody
    @DeleteMapping(value = "{id}")
    public BaseResult delete(@PathVariable("id") String id) {
        service.deleteByPrimaryKey(id);
        return BaseResult.success();
    }

    @ResponseBody
    @PutMapping(value = "{id}")
    public BaseResult update(@PathVariable("id") String id,
                            @RequestBody T updateObject) {
        updateObject.setId(Integer.valueOf(id));
        service.update(updateObject,"admin");
        return BaseResult.success();
    }
}
